﻿using FlyingEye.Consts;
using FlyingEye.Models.UserCreationOrderActionResults;
using FlyingEye.MySqlApplication;
using FlyingEye.MySqlApplication.Models;
using FlyingEye.Users.Exceptions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class UserOrderController : AbpController
    {
        private readonly IUserOrderService _userCreationOrderService;

        private readonly ILogger<UserOrderController> _logger;

        public UserOrderController(IUserOrderService userCreationOrderService,
            ILogger<UserOrderController> logger)
        {
            _userCreationOrderService = userCreationOrderService;
            _logger = logger;
        }

        /// <summary>
        /// 创建一个用户创建工单
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<CreateUserCreationOrderResult>> Create(UserCreationOrder order)
        {
            var result = new CreateUserCreationOrderResult()
            {
                IsSuccess = false
            };

            try
            {
                await _userCreationOrderService.CreateAsync(order);

                result.IsSuccess = true;
                result.Message = UserCreationOrderConsts.CreateCreateUserCreationOrderSuccess;
            }
            catch (UserAlreadyExistsException)
            {
                result.Message = AccountConsts.UserNameAlreadyBeUsed;
            }
            catch (UserCreationOrderAlreadyExistsException)
            {
                result.Message = AccountConsts.UserNameAlreadyBeUsed;
            }
            catch (InvalidPasswordException ex)
            {
                result.Message = ex.Message;
            }
            catch(InvalidUserNameException ex)
            {
                result.Message = ex.Message;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, UserCreationOrderConsts.CreateCreateUserCreationOrderError);
                result.Message = ServerConsts.ServerError;
            }

            return Ok(result);
        }

        /// <summary>
        /// 获取所有未审批的创建用户的工单
        /// </summary>
        [HttpGet]
        [Authorize(Roles = UserRoleConsts.Admin)]
        public async Task<ActionResult<GetAllUnapprovedOrdersResult>> GetAllUnapprovedOrders(int skipCount, int maxResultCount)
        {
            var result = new GetAllUnapprovedOrdersResult();

            try
            {
                result.UnapprovedUserCreationOrders = 
                    await _userCreationOrderService.GetAllUnapprovedOrdersAsync(new UnapprovedUserCreationOrderInput(skipCount, maxResultCount));
                result.IsSuccess = true;
                result.Message = UserCreationOrderConsts.GetAllUnapprovedOrdersSuccess;
            }
            catch (Exception ex)
            {
                result.Message = ServerConsts.ServerError;
                _logger.LogError(ex, UserCreationOrderConsts.GetAllUnapprovedOrdersError);
            }

            return Ok(result);
        }

        /// <summary>
        /// 审批申请创建用户的工单
        /// </summary>
        [HttpPost]
        [Authorize(Roles = UserRoleConsts.Admin)]
        public async Task<ActionResult<ApproveUserCreationOrderResult>> Approve(ApprovedUserCreationOrder order)
        {
            var result = new ApproveUserCreationOrderResult();

            try
            {
                await _userCreationOrderService.ApproveAsync(order);
                result.IsSuccess = true;
                result.Message = UserCreationOrderConsts.ApproveSuccess;
            }
            catch (UserCreationOrderNotExistException)
            {
                result.Message = UserCreationOrderConsts.UserCreationOrderNotExist;
            }
            catch (Exception ex)
            {
                result.Message = ServerConsts.ServerError;
                _logger.LogError(ex, UserCreationOrderConsts.ApprovedUserCreationOrderError);
            }

            return Ok(result);
        }
    }
}
